package com.lovecws.mumu.ffmpeg.watermark;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by Administrator on 2017/5/17.
 */
public class WatermarkService {

    //视频路径
    private String ffmpegApp;

    public String getFfmpegApp() {
        return ffmpegApp;
    }

    public void setFfmpegApp(String ffmpegApp) {
        this.ffmpegApp = ffmpegApp;
    }

    public WatermarkService(String ffmpegApp) {
        this.ffmpegApp = ffmpegApp;
    }

    public void watermark(String videoFilename,String logoFileName,String outFileName) throws IOException,InterruptedException{
        String command= ffmpegApp + " -i " + videoFilename + " -vf \"movie=" + logoFileName + " [watermark]; [in][watermark] overlay=10:10 [out]\" " + outFileName;
        Process process = Runtime.getRuntime().exec(command);
        //处理InputStream的线程
        new Thread() {
            @Override
            public void run() {
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = null;

                try {
                    while ((line = in.readLine()) != null) {
                        System.out.println("output: " + line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                String line = null;

                try {
                    while ((line = err.readLine()) != null) {
                        System.out.println("err: " + line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        err.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        process.waitFor();
        System.out.println("finish run cmd="+ command);
    }

    public void watermark2(String videoFilename, String logoFileName, String outFileName) throws IOException, InterruptedException {
        ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp,"-i", videoFilename,"-vf","movie=favicon.ico [watermark]; [in][watermark] overlay=10:10 [out]",outFileName);
        processBuilder.redirectErrorStream(true);
        Process process = processBuilder.start();
        InputStream stderr = process.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line;
        //打印 sb，获取更多信息。 如 bitrate、width、heigt
        StringBuffer sb = new StringBuffer();
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        System.out.println(sb);
        process.waitFor();
        if (br != null)
            br.close();
        if (isr != null)
            isr.close();
        if (stderr != null)
            stderr.close();
        System.out.println("finish run cmd=");
    }

}
